﻿using Microsoft.AspNetCore.Mvc;
using Sprout.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Sprout.Api.Test.Controllers
{
    public class TransactionController : ApiControllerBase
    {
        /// <summary>
        /// 测试事务
        /// </summary>
        /// <param name="threadNum"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task TestTransaction(int threadNum)
        {
            for (int i = 0; i < threadNum; i++)
            {
                TestTransaction_1(i);
            }
        }

        private async Task TestTransaction_1(int i)
        {
            Task.Run(async () =>
            {
                Thread.Sleep(2000);
                using (Transaction transaction = new Transaction())
                {

                    string six = i % 2 == 0 ? "男" : "女";
                    string sql = $"insert into excel_test(name,six,age,address,create_time)values('用户_{i + 1}','{six}','20','中国','{DateTime.Now}')";
                    //await DataService.ExecuteSqlAsync(sql);
                    transaction.Complete();
                }
                Console.WriteLine(DateTime.Now + "----" + i);

            });
        }
    }
}
